package com.jike.noobmoney.mvp.view.activity;

import android.net.Uri;
import android.view.View;
import android.widget.ImageView;
import android.widget.MediaController;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.VideoView;

import com.jike.noobmoney.R;
import com.jike.noobmoney.contants.ConstantValue;
import com.jike.noobmoney.mvp.presenter.UserPresenter;
import com.jike.noobmoney.net.IView;
import com.jike.noobmoney.util.SPUtils;
import com.jike.noobmoney.util.ToastUtils;

import butterknife.BindView;
import butterknife.OnClick;

public class VideoTwoActivity extends BaseActivity implements IView {


    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.rl_video)
    RelativeLayout rlVideo;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.videoView)
    VideoView videoView;


    private UserPresenter userPresenter;


    @Override
    protected int layoutResID() {
        return R.layout.activity_video_two;
    }

    @Override
    protected void initData() {
        tvTitle.setText("新手任务");
        userPresenter = new UserPresenter(this);

        boolean isTask = getIntent().getBooleanExtra("isTask", false);
        rlVideo.setVisibility(!isTask ? View.GONE : View.VISIBLE);


        //网络视频
        String videoUrl2 = "http://xiaobai.jikewangluo.cn/video/video.mp4";

        Uri uri = Uri.parse(videoUrl2);
        //设置视频控制器
        videoView.setMediaController(new MediaController(this));

        //播放完成回调
        videoView.setOnCompletionListener(mediaPlayer -> {
            //回调
            if (isTask) {
                //后台接口取消
                //enterMainActivity();
            }
        });

        //设置视频路径
        videoView.setVideoURI(uri);
        //开始播放视频
        videoView.start();

    }

    @OnClick({R.id.iv_back})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
        }
    }


    /**
     * 提交
     * //flag 1邀请好友  2观看视频  3关注公众号  4完成任务  5进群
     */
    private void enterMainActivity() {
        showProgress();
        String userid = SPUtils.getInstance().getString(ConstantValue.SpType.userid);
        userPresenter.newusertasksuccess(userid, "2", ConstantValue.RequestKey.new_task_finish);

    }

    @Override
    public void onSuccess(String code, String msg, Object data, String actionType) {
        if (ConstantValue.RequestKey.new_task_finish.equals(actionType)) {
            ToastUtils.showShortToastSafe("完成任务");
            setResult(RESULT_OK);
            finish();
        }
    }

    @Override
    public void onFailed(String code, String msg) {
        ToastUtils.showShortToastSafe(msg);
    }
}
